package com.dayatang.android.Activity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.dayatang.android.Db.ProjectSearchServiceDb;
import com.dayatang.android.config.Config;
import com.dayatang.android.config.TableLayoutConfig;

import android.os.Bundle;
import android.text.style.TextAppearanceSpan;
import android.view.KeyEvent;
import android.widget.TableLayout;

public class WorkloadActivity extends BaseActivity {
	
	private ProjectSearchServiceDb db;
	private TableLayout tableLayout;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.workload_activity);
		db = new ProjectSearchServiceDb(this);
		tableLayout = (TableLayout) findViewById(R.id.workload_tablelayout);
		
		InitData();
	}
	
	private void InitData() {
		try {
			
			JSONArray jsonArray = new JSONArray(db.getProjectByProjectId(getIntentParam()).getWorkItemQuantity());
			TextAppearanceSpan textappearancespan = new TextAppearanceSpan(this, R.style.normalText);   
			TextAppearanceSpan textappearancespanTitle = new TextAppearanceSpan(this, R.style.big_while);   
			TableLayoutConfig.AddTableRow(this,tableLayout,textappearancespanTitle,getString(R.string.subproject_project),getString(R.string.subproject_num),getString(R.string.subproject_unit));
			
			for (int i=0; i<jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				TableLayoutConfig.AddTableRow(this, tableLayout, textappearancespan, jsonObject.getString("workItemName"),jsonObject.getString("quantity"),jsonObject.getString("workItemUnit"));
			}
		
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	private String getIntentParam() {
		return getIntent().getStringExtra(Config.PROJID);
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			this.finish();
			finishActivityTransition();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
